package com.yuwubao.uav.tool.controller;

import com.yuwubao.framework.bean.JsonResult;
import com.yuwubao.framework.bean.PageData;
import com.yuwubao.framework.loginFilter.LoginRequired;
import com.yuwubao.uav.tool.service.OrgProjectService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Min;

/**
 * 联盟机构研究项目管理
 */
@RestController
@RequestMapping("/bk")
public class OrgProjectController {

    private static final Logger logger = LogManager.getLogger(OrgProjectController.class);
    
    @Resource
    private OrgProjectService orgProjectService;

    @LoginRequired
    @GetMapping("/clickOrgProject")
    @ApiOperation(value = "点击研究项目事件")
    public JsonResult clickOrgProject(
            @ApiParam(value = "研究项目ID", required = true) @RequestParam("id") String id
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);

            orgProjectService.clickOrgProject(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/addOrgProject")
    @ApiOperation(value = "新增研究项目")
    public JsonResult addOrgProject(
            @ApiParam(value = "研究项目标题", required = true) @RequestParam("title") String title,
            @ApiParam(value = "研究项目副标题") @RequestParam(value = "subTitle",required = false) String subTitle,
            @ApiParam(value = "内容") @RequestParam(value = "content",required = false) String content,
            @ApiParam(value = "分类ID") @RequestParam(value = "typeId",required = false) String typeId,
            @ApiParam(value = "机构ID",required = true) @RequestParam(value = "orgId") String orgId,
            @ApiParam(value = "完成时间") @RequestParam(value = "completeAt",required = false) String completeAt,
            @ApiParam(value = "发布时间") @RequestParam(value = "publicAt",required = false) String publicAt
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("content", content);
            pageData.put("typeId", typeId);
            pageData.put("publicAt", publicAt);
            pageData.put("subTitle", subTitle);
            pageData.put("title", title);
            pageData.put("orgId", orgId);
            pageData.put("completeAt", completeAt);

            orgProjectService.addOrgProject(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateOrgProject")
    @ApiOperation(value = "更新研究项目")
    public JsonResult updateOrgProject(
            @ApiParam(value = "研究项目ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "研究项目标题", required = true) @RequestParam("title") String title,
            @ApiParam(value = "研究项目副标题") @RequestParam(value = "subTitle",required = false) String subTitle,
            @ApiParam(value = "内容") @RequestParam(value = "content",required = false) String content,
            @ApiParam(value = "分类ID") @RequestParam(value = "typeId",required = false) String typeId,
            @ApiParam(value = "机构ID",required = true) @RequestParam(value = "orgId") String orgId,
            @ApiParam(value = "完成时间") @RequestParam(value = "completeAt",required = false) String completeAt,
            @ApiParam(value = "发布时间") @RequestParam(value = "publicAt",required = false) String publicAt
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("content", content);
            pageData.put("typeId", typeId);
            pageData.put("publicAt", publicAt);
            pageData.put("subTitle", subTitle);
            pageData.put("title", title);
            pageData.put("orgId", orgId);
            pageData.put("completeAt", completeAt);
            orgProjectService.updateOrgProject(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteOrgProject")
    @ApiOperation(value = "删除研究项目")
    public JsonResult deleteOrgProject(
            @ApiParam(value = "研究项目ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);

            orgProjectService.deleteOrgProject(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/disableOrEnableOrgProject")
    @ApiOperation(value = "禁用或启用研究项目")
    public JsonResult disableOrEnableOrgProject(
            @ApiParam(value = "研究项目ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            orgProjectService.disableOrEnableOrgProject(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listOrgProject")
    @ApiOperation(value = "获取研究项目列表")
    public JsonResult listOrgProject(
            @ApiParam(value = "分类ID") @RequestParam(value = "typeId",required = false) String typeId,
            @ApiParam(value = "机构ID") @RequestParam(value = "orgId",required = false) String orgId,
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("orgId", orgId);
            pageData.put("typeId", typeId);
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(orgProjectService.listOrgProject(pageData));
            result.setResultNum(orgProjectService.listOrgProjectCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/addOrgProjectType")
    @ApiOperation(value = "新增研究项目类型")
    public JsonResult addOrgProjectType(
            @ApiParam(value = "机构ID",required = true) @RequestParam(value = "orgId") String orgId,
            @ApiParam(value = "研究项目类型,多个用逗号分隔", required = true) @RequestParam("names") String names,
            @ApiParam(value = "研究项目类型备注") @RequestParam(value = "remark",required = false) String remark
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("orgId", orgId);
            pageData.put("names", names);
            pageData.put("remark", remark);

            orgProjectService.addOrgProjectType(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateOrgProjectType")
    @ApiOperation(value = "更新研究项目类型")
    public JsonResult updateOrgProjectType(
            @ApiParam(value = "研究项目类型ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "机构ID",required = true) @RequestParam(value = "orgId") String orgId,
            @ApiParam(value = "研究项目类型名称", required = true) @RequestParam("name") String name,
            @ApiParam(value = "研究项目类型备注") @RequestParam(value = "remark",required = false) String remark
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("name", name);
            pageData.put("orgId", orgId);
            pageData.put("remark", remark);

            orgProjectService.updateOrgProjectType(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteOrgProjectType")
    @ApiOperation(value = "删除研究项目类型")
    public JsonResult deleteOrgProjectType(
            @ApiParam(value = "研究项目类型ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            orgProjectService.deleteOrgProjectType(pageData);
            result.setSuccessful();
        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/disableOrEnableOrgProjectType")
    @ApiOperation(value = "禁用或启用研究项目")
    public JsonResult disableOrEnableOrgProjectType(
            @ApiParam(value = "ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            orgProjectService.disableOrEnableOrgProjectType(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listOrgProjectType")
    @ApiOperation(value = "获取研究项目类型列表")
    public JsonResult listOrgProjectType(
            @ApiParam(value = "机构ID") @RequestParam(value = "orgId",required = false) String orgId,
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("orgId", orgId);
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(orgProjectService.listOrgProjectType(pageData));
            result.setResultNum(orgProjectService.listOrgProjectTypeCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @GetMapping("/listOrgProjectTypeAll")
    @ApiOperation(value = "获取研究项目类型列表所有")
    public JsonResult listOrgProjectTypeAll(
            @ApiParam(value = "机构ID") @RequestParam(value = "orgId",required = false) String orgId,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("orgId", orgId);
            pageData.put("searchKey", searchKey);

            result.setResult(orgProjectService.listOrgProjectTypeAll(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }




}
